package com.mmall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IOrderService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manage/order")
public class OrderManageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IOrderService iOrderService;

    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> orderList(HttpSession session,
                                              @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){

        ServerResponse adminResponse = checkAdminRole(session);
        if(adminResponse.isSuccess()){
            return iOrderService.manageList(pageNum, pageSize);
        }else{
            return adminResponse;
        }
    }

    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse orderDetail(HttpSession session, Long orderNo){
        ServerResponse adminResponse = checkAdminRole(session);
        if(adminResponse.isSuccess()){
            return iOrderService.manageDetail(orderNo);
        }else{
            return adminResponse;
        }
    }

    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse orderSearch(HttpSession session, Long orderNo,
                                      @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        ServerResponse adminResponse = checkAdminRole(session);
        if(adminResponse.isSuccess()){
            return iOrderService.manageSearch(orderNo,pageNum,pageSize);
        }else{
            return adminResponse;
        }
    }


    @RequestMapping("send_goods.do")
    @ResponseBody
    public ServerResponse orderSendGoods(HttpSession session, Long orderNo){
        ServerResponse adminResponse = checkAdminRole(session);
        if(adminResponse.isSuccess()){
            return iOrderService.manageSendGoods(orderNo);
        }else{
            return adminResponse;
        }
    }

    /**
     * 私有方法, 抽取重复操作
     * 1: 验证用户是否登录; 2: 验证是否具有管理员权限
     *
     * @param session
     * @return
     */
    private ServerResponse checkAdminRole(HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        return iUserService.checkAdminRole(user);
    }
}
